package com.cwj.film.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cwj.common.domain.film;
import com.cwj.common.utils.utils;
import com.cwj.film.Listener.Beans;
import com.cwj.film.service.inter.index;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Service
public class indexImpl implements index {
    @Resource
    private com.cwj.film.dao.filmDao filmDao;
    @Resource
    private Beans beans;
    private  final Random random=new Random();
    private List<film> GetFilmList(Integer size){
        List<film> films;
        Integer count = filmDao.selectCount(null);
        if(count>size){
            films=new ArrayList<>(size);
            Set<Integer> randomSet=new HashSet<>((int)Math.ceil(size/0.75));
            QueryWrapper<film> queryWrapper=new QueryWrapper<>();
            while (randomSet.size()<size){ randomSet.add(random.nextInt(count)); }
            for (Integer integer : randomSet) {
                queryWrapper.last("limit "+integer+" , "+" 1").select("title","type");
                films.add(filmDao.selectOne(queryWrapper));
                queryWrapper.clear();
            }
        }else {
            films= filmDao.selectList(new QueryWrapper<film>().select("title","type"));
        }
        return films;
    }

    @Override
    public List<film> GetFilmBigPictList(Integer size) throws InvocationTargetException, IllegalAccessException {

        List<film> films = GetFilmList(size);
    utils.GetFilmPictList(films,1740,1036,beans.getServerPath());
        Collections.shuffle(films);
      return films;
    }

    @Override
    public List<film> GetFilmLittlePictList(Integer size) throws InvocationTargetException, IllegalAccessException {
        List<film> films = GetFilmList(size);
        utils.GetFilmPictList(films,540,488,beans.getServerPath());
        Collections.shuffle(films);
        return films;
    }

    @Override
    public Map<String, List<film>> GetFilmTitleMapByTypeMap(Map<String,Integer> map) {
        Map<String,List<film>> filmMap=new HashMap<>((int) Math.ceil(map.size()/0.75));
        QueryWrapper<film> queryWrapper=new QueryWrapper<>();
        for (String type : map.keySet()) {
            queryWrapper.clear();
            Integer size = map.get(type);
            Integer count = filmDao.selectCount(queryWrapper.eq("type", type));
            List<film> films ;
            if(size>=count){
               films= filmDao.selectList(queryWrapper.select("title","type","release_date"));
            }else {
                films=new ArrayList<>(size);
                Set<Integer> randRomSet=new HashSet<>((int) Math.ceil(size/0.75));
                while (randRomSet.size()<size){ randRomSet.add(random.nextInt(count)); }
                for (Integer integer : randRomSet) {
                    queryWrapper.clear();
                    queryWrapper.eq("type",type).select("type","title","release_date").last("limit " + integer + " , 1");
                    film film = filmDao.selectOne(queryWrapper);
                    films.add(film);
                      }
                  }
            Collections.shuffle(films);
            filmMap.put(type,films);
        }
        return filmMap;
    }
}
